<template>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div class="header-left">
          <img src="../assets/default.png" alt=""> 
            <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="loginOut">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="collapseBtn" @click="toCollapse">|||</div>
          <el-menu background-color="#333744" text-color="#fff" active-text-color="#ffd04b" 
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
            <!-- 一级菜单 -->
            <el-submenu :index=" '/' + item1.path" v-for="item1 in menuList" :key="item1.id">
              <template slot="title">
                <i :class="iconsObj[item1.id]" style="margin-right: 10px;"></i>
                <span>{{ item1.authName }}</span>
              </template>
              <!-- 二级菜单 -->
               <el-menu-item :index="'/' + item2.path" @click="saveNavState('/' + item2.path)" v-for="item2 in item1.children" :key="item2.id">
                <template slot="title" >
                  <i class="el-icon-menu"></i>
                  <span>{{ item2.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧页面主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
    created() {
      this.getMenus()
      this.activePath = window.sessionStorage.getItem('path')
        
    },
    data() {
        return {
          menuList:[],
          iconsObj:{
            '125':'iconfont icon-user',
            '103':'iconfont icon-tijikongjian',
            '101':'iconfont icon-shangpin',
            '102':'iconfont icon-danju',
            '145':'iconfont icon-baobiao',
          },
          isCollapse:false,
          // 被激活的链接地址
          activePath:''

            
        }
    },
    methods: {
        loginOut(){
            window.sessionStorage.removeItem('token');
            this.$router.push('/login')
        },
        async getMenus(){
          const {data : res} = await this.$http.get('menus')
          if(res.meta.status !== 200) return this.$message.error('获取菜单列表失败')
          this.menuList = res.data
        
          
        },
        // 切换菜单的折叠与展开
        toCollapse(){
          this.isCollapse = !this.isCollapse
        },
        saveNavState(path){
          window.sessionStorage.setItem('path',path)
          this.activePath = path
        }
        
    },

}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  align-items: center;
  justify-content: space-between;
  img{
    width: 50px;
    height: 50px;
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #eaedf1;
}
.header-left{
  display: flex;
  align-items: center;
  span{
    color: #fff;
    margin-left: 15px;
  }
}
.collapseBtn{
  color: #fff;
  text-align: center;
  cursor: pointer;
  font-size: 12px;
  letter-spacing: 0.2;
  line-height: 24px;
}
</style>